<?php

namespace serv\queue\warehouse;

use L;
use of;
use Exception;
use common\commonUpdateTrait;
use common\batchInsert;
use of\base\com\mq;
use Sdk\Yqn\callYqnApiUtil;
use traits\StockTrait;

class yqn
{
    //同步运去哪的库存
    public static function pull($arr)
    {
        try {
            $arr = $arr['data'];
            $action = '/v2/inventory/get_inventory_by_page';
            $method = 'GET';
            $page_size = 200;
            $current_page = 1;
            do {
                $data = [
                    'warehouseCodes' => $arr['warehouse_code'],
                    'page' => $current_page,
                    'rows' => $page_size,
                    'sidx' => 'id',
                    'sort' => 'desc'
                ];
                //查询库存信息
                $result = callYqnApiUtil::callYqnApi($action, $method, $data);
                if (!empty($result['data']['content'])) {
                    //只负责取数据，不负责处理
                    $stockArr = $result['data']['content'];
                    $total_page = $result['data']['totalPages'];
                    //把查询到的数据交给别一个方法处理
                    foreach ($stockArr as $k => $val) {
                        $key = "check_yqn_stock_{$arr['warehouse_id']}_{$val['sku']}";
                        $val = array_merge($val, ['warehouse_id' => $arr['warehouse_id']]);
                        //echo json_encode($val, JSON_UNESCAPED_UNICODE); exit;
                        mq::set(["checkYqnStock", $key, $k], $val);
                    }
                } else {
                    $total_page = 1;
                }
                $current_page++;
            } while ($current_page <= $total_page);
            return true;
        } catch (Exception $exp) {
            return false;
        }
    }





    //更新库存或者写入新的库存数据
    public static function check($arr)
    {
        set_time_limit(0);
        try {
            of::work(['warehouse']);
            $val = $arr['data'];
            $sql = "SELECT stock_id, sku FROM stock WHERE 1 
                    AND warehouse_id = '{$val['warehouse_id']}' 
                    AND sku_code = '{$val['sku']}';";
            $result = L::sql($sql, 'warehouse');
            if (!empty($result)) {
                //更新数据
                $data = [
                    'sku' => StockTrait::tran2sku($val['sku']),
                    'available_stock' => $val['inventory_available'], //可用
                    'lock_stock' => $val['inventory_reserved'], //锁
                    'stock' => $val['inventory_available'] + $val['inventory_reserved'], //在库
                    'on_way_stock' => $val['inventory_transport'],  //在途？
                    'status' => (0 == $val['inventory_type']) ? 1 : 0,
                    'belong' => of::config('env.PUBLIC_BELONG'),
                    'updated_by' => of::config('env.SYSTEM_ID'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
                //更新
                commonUpdateTrait::update('stock', $data, $result[0]['stock_id'], 'stock_id');
            } else {
                //插入数据
                $position = self::position($val['warehouse_id']);
                $data[] = [
                    'position_id' => !empty($position) ? $position['position_id'] : 1,
                    'area_id' => !empty($position) ? $position['area_id'] : 1,
                    'warehouse_id' => $val['warehouse_id'],
                    'sku' => StockTrait::tran2sku($val['sku']),
                    'sku_code' => $val['sku'],
                    'sku_name' => $val['product_line_name'],
                    'sku_name_en' => $val['product_line_name'],
                    'stock' => $val['inventory_available'] + $val['inventory_reserved'],
                    'available_stock' => $val['inventory_available'],
                    'lock_stock' => $val['inventory_reserved'],
                    'on_way_stock' => $val['inventory_transport'], //在途
                    'status' => (0 == $val['inventory_type']) ? 1 : 0,
                    'belong' => of::config('env.PUBLIC_BELONG'),
                    'created_by' => of::config('env.SYSTEM_ID'),
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_by' => of::config('env.SYSTEM_ID'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
                $sql = batchInsert::insert('stock', $data);
                L::sql($sql, 'warehouse');
            }
            of::work(true);
            return true;
        } catch (Exception $exp) {
            of::work($exp);
            return false;
        }
    }

    //获取此仓库默认库位
    private static function position($warehouse_id)
    {
        $sql = "SELECT position_id, area_id FROM `position` WHERE 1 AND warehouse_id = '{$warehouse_id}';";
        $rows = L::sql($sql, 'warehouse');
        return (!empty($rows)) ? $rows[0] : [];
    }
}